package com.example.kehoobackend.controller;

import com.example.kehoobackend.common.CommonResponse;
import com.example.kehoobackend.config.WebSocket;
import com.example.kehoobackend.domain.Message;
import com.example.kehoobackend.domain.User;
import com.example.kehoobackend.service.MessageService;
import com.example.kehoobackend.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
public class MessageController {

    @Resource
    private WebSocket webSocket;

    @Autowired
    private UserService userService;

    @Autowired
    private MessageService messageService;


    /**
     * 获取当前用户聊过天的用户列表
     *
     * @param user_id 用户ID
     * @return CommonResponse<List < User>>
     */
    @GetMapping("/message/users")
    public CommonResponse<List<User>> getChattedUsers(
            @RequestParam("user_id") Integer user_id) {
        return CommonResponse.createForSuccess("获取成功", messageService.getChattedUsers(user_id));
    }

    /**
     * 获取当前用户与指定用户的聊天记录
     *
     * @param src_id  当前用户ID
     * @param target_id 指定用户ID
     * @return CommonResponse<List < Message>>
     */
    @GetMapping("/message/records")
    public CommonResponse<List<Message>> getMessageRecord(
            @RequestParam("src_id") Integer src_id,
            @RequestParam("target_id") Integer target_id) {
        return CommonResponse.createForSuccess("获取成功", messageService.getMessageRecord(src_id, target_id));
    }

    /**
     * 判断当前用户与指定用户是否有聊天记录
     *
     * @param src_id  当前用户ID
     * @param target_id 指定用户ID
     * @return CommonResponse<Boolean>
     */
    @GetMapping("/message/chatted")
    public CommonResponse<Boolean> hasRecord(
            @RequestParam("src_id") Integer src_id,
            @RequestParam("target_id") Integer target_id) {
        return CommonResponse.createForSuccess("获取成功", messageService.isChatted(src_id, target_id));
    }




    /**
     * 新增聊天关系
     *
     * @param src_id  当前用户ID
     * @param target_id 指定用户ID
     * @return CommonResponse<Boolean>
     */
    @PostMapping("/message/add")
    public CommonResponse<Boolean> addChatRelation(
            @RequestParam("src_id") Integer src_id,
            @RequestParam("target_id") Integer target_id) {
        return messageService.addChatRelation(src_id, target_id)
                ? CommonResponse.createForSuccess("添加成功", true)
                : CommonResponse.createForSuccess("添加失败", false);
    }


    /**
     * 给所有用户发送消息
     *
     * @param content 消息内容
     * @return CommonResponse<Boolean>
     */
    @PostMapping("/message/all")
    public CommonResponse<Boolean> sendAll(
            @RequestParam("content") String content) {
        //推送消息暂时未保存
        boolean flag = webSocket.sendAllMessage(content);
        return flag
                ? CommonResponse.createForSuccess("发送成功", true)
                : CommonResponse.createForSuccess("发送失败", false);
    }

    /**
     * 给指定用户发送消息
     *
     * @param message 消息实体
     * @return CommonResponse<Boolean>
     */
    @PostMapping("/message/one")
    public CommonResponse<Boolean> sendOne(
            @RequestBody Message message) {
        boolean flag = messageService.saveMessageRecord(message);
        webSocket.sendOneMessage(message);
        return flag
                ? CommonResponse.createForSuccess("发送成功", true)
                : CommonResponse.createForSuccess("发送失败", false);
    }


}
